﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace Mall.Order
{
    public partial class Order_End : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            MODEL.MOrder model_ordere = new MODEL.MOrder();
            BLL.Models.MemberModels models_member = new BLL.Models.MemberModels();
            string username = models_member.CheckUserIdOrder();// CheckUserIdOrder();
            model_ordere = (MODEL.MOrder)Session["model_order"];
            if (Session["model_order"] != null)
            {

                model_ordere.Userid = Convert.ToInt32(models_member.GetUserIdByName(username));
                username = DateTime.Now.ToString("yyyyMMddhhmmss");
                Random rnd = new Random();
                username = username + rnd.Next(100000).ToString();
                model_ordere.OrderNum = username + rnd.Next(100000, 999999).ToString();
                BLL.Models.MOrderModels models = new BLL.Models.MOrderModels();
                model_ordere.OrderTime = DateTime.Now;
                models.CreateOrder(model_ordere);
                //处理定单详情
                DataTable dt = new DataTable();
                dt = models.GetOrderByFiter("ordernum='" + model_ordere.OrderNum.ToString() + "' ");
                string orderid = dt.Rows[0]["id"].ToString();
                string buylist = string.Empty;
                buylist = Request.Cookies["cart"] == null ? "" : Request.Cookies["cart"].Value.ToString();
                buylist = Server.UrlDecode(buylist);
                string[] plist = buylist.Split(new char[] { ',' });
                if (plist.Length == 0)
                {

                    plist[0] = buylist;
                }
                for (int i = 0; i < plist.Length; i++)
                {

                    string s = plist[i].ToString();
                    if (s.IndexOf("{$s$}") < 0)
                    {

                        continue;
                    }
                    string[] pInfos = s.Split(new string[] { "{$s$}" }, StringSplitOptions.None);
                    string priductid = pInfos[0];
                    string[] tempstrs = pInfos[1].Split(new string[] { "{$yc$}" }, StringSplitOptions.None);
                    string Pcount = tempstrs[0];
                    tempstrs = tempstrs[1].Replace(" ", "").Split(new string[] { "\"\"" }, StringSplitOptions.None);
                    string yanse = tempstrs[0].Replace("\"", "");
                    string cima = tempstrs[1].Replace("\"", "");
                    string productinfomore = yanse + "{$yc$}" + cima;
                    MODEL.MorderMore model_mordermore = new MODEL.MorderMore();
                    model_mordermore.OrderId = Convert.ToInt32(orderid);
                    model_mordermore.Productid = Convert.ToInt32(priductid);
                    model_mordermore.OrderTiem = DateTime.Now;
                    model_mordermore.SessionId = "j";
                    model_mordermore.Content = "j";
                    model_mordermore.Intro = "j";
                    model_mordermore.Price = Convert.ToDecimal("0.0");
                    model_mordermore.Point = 1;
                    model_mordermore.Count = Convert.ToInt32(Pcount);
                    model_mordermore.ProductMoreInfo = productinfomore;
                    models.CreateOrderMore(model_mordermore);

                }

            }

            Response.Cookies["cart"].Expires = DateTime.Now;

        }
    }
}